package com.myteam.fugaocrm.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myteam.fugaocrm.common.ApplicationContextAware;
import com.myteam.fugaocrm.modle.FgPartnerCompanyDetail;
import com.myteam.fugaocrm.modle.FgRemindMessage;
import com.myteam.fugaocrm.modle.FgTask;
import com.myteam.fugaocrm.sqldb.mapper.FgPartnerCompanyDetailMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgRemindMessageMapper;
import com.myteam.fugaocrm.sqldb.mapper.FgTaskMapper;

/**
 * 合作商付款提醒
 * @author BZX
 *
 */
public class RemindPartnerPay {

	private FgTaskMapper fgTaskMapper = (FgTaskMapper) ApplicationContextAware.getApplicationContext().getBean(FgTaskMapper.class);
	private FgRemindMessageMapper fgRemindMessageMapper = (FgRemindMessageMapper) ApplicationContextAware.getApplicationContext().getBean(FgRemindMessageMapper.class);
	private FgPartnerCompanyDetailMapper fgPartnerCompanyDetailMapper = (FgPartnerCompanyDetailMapper) ApplicationContextAware.getApplicationContext().getBean(FgPartnerCompanyDetailMapper.class);
	
	public void work() {
		try {
        	//批量查询任务
        	List<FgTask> taskList = fgTaskMapper.all();
          
        	//依次循环执行任务
        	for(FgTask task : taskList) {
        		//售前日志
        		if(task.getTaskId() == 2 && task.getIsDelete() == 0) {
        			
        			List<FgPartnerCompanyDetail> FgPartnerCompanyDetailList = fgPartnerCompanyDetailMapper.selectAllWaitPay();//新查的全部
        			List<FgRemindMessage> fgRemindMessageList = fgRemindMessageMapper.selectRemindByTypeId(2);//旧数据
        			
        			int iSize = fgRemindMessageList.size();
        			int jSize = FgPartnerCompanyDetailList.size();
        			
        			int state = -1;//-1:删除|0:不变|1:新增
        			List<Integer> delList = new ArrayList<Integer>();//待删除集合
        			List<FgPartnerCompanyDetail> nothingList = new ArrayList<FgPartnerCompanyDetail>();//无变动集合
        			
        			for(int i=0; i<iSize; i++) {
        				FgRemindMessage temp = fgRemindMessageList.get(i);
        				
        				for(int j=0; j<jSize; j++) {
        					if(temp.getPartnerDetailId() == FgPartnerCompanyDetailList.get(j).getPartnerDetailId()) {
        						nothingList.add(FgPartnerCompanyDetailList.get(j));
        						state = 0;
        						break;
        					}
        				}
        				
        				if(state == -1) {
        					delList.add(temp.getPartnerDetailId());
        				}else {
        					state = -1;
        				}
        			}
        			
        			//过滤新增
        			for(FgPartnerCompanyDetail temp : nothingList) {
        				FgPartnerCompanyDetailList.remove(temp);
        			}
        			
        			//处理删除
        			for(Integer partnerDetailId : delList) {
        				fgRemindMessageMapper.deletePartnerDetail(partnerDetailId);
        			}
        			
        			//处理新增
        			for(FgPartnerCompanyDetail temp : FgPartnerCompanyDetailList) {
        				Map<String, Object> param = new HashMap<String,Object>();
        				param.put("typeId", 2);
        				param.put("partnerDetailId", temp.getPartnerDetailId());
        				param.put("projectId", temp.getProjectId());
        				param.put("remindContent", "【" + temp.getProjectName() + "】项目有合作商付款事项");
        				param.put("modifiedDate", new Date());
        				
        				fgRemindMessageMapper.insertPartnerDetail(param);
        			}
        		}
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}
